var _ajax = require('../../utils/ajax');
var _Server = require('../../utils/server');
var total_micro_second;
var setTime;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        producStartTime: '',
        producEndTime: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this;
        _ajax.orderCourseDetails(options.orderId, function(data) {
            var data = data.data;
            console.log(data);
            that.setData({
                indexUrl: _Server.url,
                myCourseDataDetails: data,
            });
            var startTime = data.product_start_time;
            var endTime = data.product_end_time;
            // var serverStartTime = data.server_start_time;
            // var serverEndTime = data.server_end_time
            total_micro_second = endTime * 1000 - (Date.parse(new Date()));
            clearTimeout(setTime);
            that.countdown(this);
            that.setData({
                producStartTime: that.timestampToTime(data.product_start_time),
                producEndTime: that.timestampToTime(data.product_end_time),
                serverStartTime: that.timestampToTime(data.server_start_time),
                serverEndTime: that.timestampToTime(data.server_end_time),
            });
        }, this);
    },
    countdown: function() {
        var that = this;
        // 渲染倒计时时钟  
        that.dateformat(that, total_micro_second);
        if (total_micro_second <= 0) {
            that.setData({
                clockTimeout: false,
                clock: "秒杀结束"
            });
            // timeout则跳出递归  
            // console.log('结束')
            return;
        }
        //settimeout实现倒计时效果  
        setTime = setTimeout(function() {
            // 放在最后--  
            total_micro_second -= 100;
            that.countdown(that);
        }, 100) //精确到0.1s即100ms  
    },
    dateformat: function(that, micro_second) { // 渲染时间
        var that = this;
        // 总秒数  
        var second = Math.floor(micro_second / 1000);
        // 天数  
        var day = Math.floor(second / 3600 / 24);
        // 总小时  
        var hr = Math.floor(second / 3600);
        // 小时位  
        var hr2 = hr % 24;
        // 分钟位  
        var min = Math.floor((second - hr * 3600) / 60);
        // 秒位  
        var sec = (second - hr * 3600 - min * 60); // equal to => var sec = second % 60;  
        // 毫秒位，保留2位  
        var micro_sec = Math.floor((micro_second % 1000) / 10);

        second = that.addZero(second);
        day = that.addZero(day);
        hr2 = that.addZero(hr2);
        min = that.addZero(min);

        that.setData({
            day: day,
            hour: hr2,
            minute: min,
            second: sec
        });
    },
    addZero: function(num) {
        var numString;
        if (num < 10) {
            numString = '0' + num;
        } else {
            numString = num;
        }
        return numString;
    },
    timestampToTime: function(timestamp) { // 时间戳转化
        var date = new Date(timestamp * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
        var Y = date.getFullYear() + '-';
        var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
        var D = date.getDate() + ' ';
        var h = date.getHours() + ':';
        var m = date.getMinutes() + ':';
        var s = date.getSeconds();
        return Y + M + D;
    },
    fnProductDetails: function(e) { // 套餐详情
        var that = this;
        var productId = e.currentTarget.dataset.productid;

        wx.navigateTo({
            url: '../myProductDetails/myProductDetails?productId=' + productId + '&courseId=' + that.data.myCourseDataDetails.course_id
        });
    },
    fnRushBuy: function(e) { // 抢购.付款
        var that = this;
        var productId = e.currentTarget.dataset.productid;;
        _ajax.myProductPayOrder(that.data.myCourseDataDetails.course_id, productId, function(data) {
            console.log(data);
            if (data.code == 0) {
                wx.navigateTo({
                    url: '../wxPayProduct/wxPayProduct?orderId=' + data.data.order_id + '&productId=' + productId
                });
            } else {
                setTimeout(function(){
                    wx.showToast({
                        title: data.msg,
                        icon: 'none'
                    });
                },500);
            }
        }, this);
    }
})